<?php
class Load_Type
{
	const Normal = 1;
	const Lazy =2;
} 
  
class Fetch_Image_Model
{
	const OnlySrc = 1;
	const All = 2 ; 
}

class Model_Type 
{
	const DB = 'db';   
}


class base extends JR_Model
{
	protected $table_name = null;
	
	protected function get_class_name()
	{
		$manager_class_name = get_class($this);
		$manager_class_name_segs = explode("_", $manager_class_name);
		array_pop($manager_class_name_segs);
		return implode("_", $manager_class_name_segs);
	}
	
	protected function get_table_name()
	{
		return $this->get_class_name();
	}
	
	function __construct()
	{
		$this->table_name = $this->get_table_name(); 
	}

	function convert($db_object,$model_class_name = null)
	{
		if($model_class_name == null)
		{
			$model_class_name = $this->get_class_name()."_model";
		}
		
		//echo $model_class_name . " " . get_class($this) . "<br>";
		
		$model_obj = new $model_class_name();
		
		foreach($db_object as $key=>$value)
		{
			$model_obj->$key = $value;
		}
		return $model_obj;
	}
	
	function convert_list($db_objects,$model_class_name = null)
	{
		if($model_class_name == null)
		{
			$model_class_name = $this->get_class_name()."_model";
		}
		
		$model_objects = Array();
		foreach($db_objects as $db_object)
		{
			$model_objects[] = $this->convert($db_object,$model_class_name);
		}
		return $model_objects;  
	}
	
	function build_where_string($where)
	{
		$conditions = Array(); 
		foreach($where as $key=>$value)
		{
			if($value=="" || empty($value) || $key =='tab') continue;
			//echo strpos($value,'between');
			if( strpos($value,'>')===false &&
					strpos($value,'<')===false &&
					strpos($value,'!=')===false &&
					strpos($value,'is not null')===false &&
					strpos($value,'between')===false &&
					strpos($value,'in(')===false &&
					strpos($value,'like')===false
			)
			{
				$conditions[] = "and ".$key."='".$value."'";
			}
			else
			{
				$conditions[] = "and ".$key." ".$value;
			}
		}
		
		if(sizeof($conditions)>0)
		{
			return implode(" ", $conditions); 
		}
		return "";
	}
}

class base_model extends base
{
	function __construct($model_type = Model_Type::DB)
	{
		parent::__construct();
		$self = $this->get_class_name(); 
		$this->$self = $this;
		
		if($model_type == Model_Type::DB) 
		{
			$ci = &get_instance();
			if(!isset($ci->fields[$this->table_name]))
			{
				$ci->fields[$this->table_name] = $this->db->field_data($this->table_name); 
			}
			$fields = $ci->fields[$this->table_name];  
			// print_r($fields);exit; 
			foreach ($fields as $field)
			{
			    $f_name = $field->name;
				$this->$f_name = $field->default;
			}
		}
	} 
	
	function __get($property_name)
	{
		if(!isset($this->$property_name))
		{
			$get_method= "get_".$property_name; 
			
			if(method_exists($this,$get_method))
			{  
				$this->$property_name = $this->$get_method();
			}
			else
			{
				/*
				$traces = debug_backtrace();
				$trace = null;
				if(sizeof($traces)>0)
				{
					$trace = $traces[0]; 
				}
				// var_dump($traces); 
				die("Fatal Error:".$property_name." not existed. Error in file:".$trace['file']." line:".$trace['line']); 
				*/ 
				return parent::__get($property_name);
			}  
		}
		return $this->$property_name;
	} 
	
	function merge($ob)
	{
		foreach($ob as $key => $value)
		{
			$this->$key = $value;
		}
	}
}

class base_manager extends base
{
	public function get($params,$load_type = Load_Type::Lazy)
	{
		if(is_array($params))
		{
			return $this->convert(
					$this->db->get_where($this->table_name,$params)->row()
			);
		}
		else
		{
			return $this->convert(
					$this->db->get_where($this->table_name,
							array('id'=>$params)
					)->row()
			);
		}
	}
	
	function insert($obj)
	{
		$this->db->insert($this->table_name,$obj); 
		return $this->db->insert_id();     
	}
	
	function update($obj,$params)
	{
		if(is_array($params))
		{
			$this->db->update($this->table_name,$obj,$params);
		}
		else
		{
			$this->db->update($this->table_name,$obj,array('id'=>$params));
		}
	}
	
	function delete($params)
	{
		if(is_array($params))
		{
			$this->db->delete($this->table_name,$params);
		}
		else
		{
			$this->db->delete($this->table_name,array('id'=>$params));
		}
	}
}